<?php

namespace common\models;

use common\Service\RedisService;
use Yii;


class Sitemap
{
    /**
     * 站长地图数据
     * @param int $type  1:百度 360 2：谷歌
     * @return array
     */
    public static function load($type = 1){
        $rs = [];

        $rs = array_merge($rs,static::menuUrls());
        $rs = array_merge($rs,static::tagUrls());

        if($type == 2){
            $rs = array_merge($rs,static::starUrls());
            $rs = array_merge($rs,static::portrayUrls());
            $rs = array_column($rs,'loc');

            $_rs = [];
            foreach ($rs as $r){
                $_rs[] = ['loc' => $r];
            }
            return $_rs;
        }
        return $rs;
    }

    public static function newsUrls($maxNewsId = 0){
        $minNewsId = 666;
        $maxNewsId == 0 && $maxNewsId = News::find()->max('id');

        $result = [];
        for ($i = $minNewsId ; $i < $maxNewsId ; $i++){
            $newInfo = RedisService::getHash('news:'.$i,['create_time']);
            if(!empty($newInfo) && isset($newInfo['create_time']) && intval($newInfo['create_time']) > 0)
                $result[] = [
                    'loc' => "http://www.guotuw.com/news/{$i}.html",
                    'lastmod' => date('Y-m-d',$newInfo['create_time']),
                    'changefreq' => 'daily',
                    'priority' => 1,
                ];
        }
        return $result;
    }

    public static function starUrls(){
        $result = [];
        $minStarId = 16666;
        $maxStarId = Star::find()->max('id');

        for($i = $minStarId ; $i < $maxStarId ; $i++){
            $exists = (int)Yii::$app->get('redis')->exists('star:'.$i);
//            $starInfo = RedisService::getHash('star:'.$i,['create_time']);
            if($exists == 1){
                $result[] = [
                    'loc' => " http://www.guotuw.com/meinv/{$i}.html",
//                    'lastmod' => '2017-12-15',
                    'changefreq' => 'daily',
                    'priority' => 1,
                ];
            }
        }

        return $result;
    }

    public static function portrayUrls(){
        $result = [];
        $minPortrayId = 666;
        $maxPortrayId = Portray::find()->max('id');

        for($i = $minPortrayId ; $i < $maxPortrayId ; $i++){

            $portrayInfo = RedisService::getHash('portray:'.$i,['create_time']);
            if(!empty($portrayInfo) && isset($portrayInfo['create_time']) && intval($portrayInfo['create_time']) > 0){
                $result[] = [
                    'loc' => " http://www.guotuw.com/xiezhen/{$i}.html",
                    'lastmod' => date('Y-m-d',$portrayInfo['create_time']),
                    'changefreq' => 'daily',
                    'priority' => 1,
                ];
            }
        }

        return $result;
    }

    public static function menuUrls(){
        $menuUrls = [
            'meinv',
            'xiezhen',
            'ranking',
            'ranking/hot',
            'ranking/japan',
            'ranking/mainland',
            'ranking/taiwan',
            'ranking/korea',
            'ranking/hybrid',
            'ranking/model',
            'ranking/girl',
            'ranking/week',
            'ranking/month',
            'news',
        ];

        $result = [];
        foreach ($menuUrls as $url){
            $result[] = [
                'loc' => "http://www.guotuw.com/{$url}/",
//                'lastmod' => '2017-12-14',
                'changefreq' => 'daily',
                'priority' => 0.9,
            ];
        }

        return $result;
    }


    public static function tagUrls(){


        $minTagId = Tags::find()->min('id');
        $maxTagId = Tags::find()->max('id');
//        $otherTagIds = [451,516];



        $result = $tagUrls = $searchUrls = [];
        for ($i = $minTagId ; $i <= $maxTagId ; $i++){
            $tagInfo = Tags::tagInfoById($i);

            if(!empty($tagInfo)){
                //分类标签
                if($tagInfo['category'] == 0){ //通用
                    $tagUrls[] = "meinv/{$tagInfo['alias']}";
                    $tagUrls[] = "xiezhen/{$tagInfo['alias']}";
                }else if($tagInfo['category'] == 1){ //美女标签
                    $tagUrls[] = "meinv/{$tagInfo['alias']}";
                }else if($tagInfo['category'] == 2){ //写真标签
                    $tagUrls[] = "xiezhen/{$tagInfo['alias']}";
                }
                //搜索标签
                $searchUrls[] = !empty($tagInfo['show_name']) ? $tagInfo['show_name'] : $tagInfo['name'];
            }
        }

        foreach ($tagUrls as $url){
            $result[] = [
                'loc' => "http://www.guotuw.com/{$url}/",
//                'lastmod' => '2017-12-14',
                'changefreq' => 'daily',
                'priority' => 0.9,
            ];
        }
        unset($tagUrls);

        foreach ($searchUrls as $url){
            $result[] = [
                'loc' => " http://www.guotuw.com/search/?keyword={$url}",
//                'lastmod' => '2017-12-14',
                'changefreq' => 'daily',
                'priority' => 0.9,
            ];
            $result[] = [
                'loc' => " http://www.guotuw.com/search/xiezhen/?keyword={$url}",
//                'lastmod' => '2017-12-14',
                'changefreq' => 'daily',
                'priority' => 0.9,
            ];
            $result[] = [
                'loc' => " http://www.guotuw.com/search/meinv/?keyword={$url}",
//                'lastmod' => '2017-12-14',
                'changefreq' => 'daily',
                'priority' => 0.9,
            ];
            $result[] = [
                'loc' => " http://www.guotuw.com/search/news/?keyword={$url}",
//                'lastmod' => '2017-12-14',
                'changefreq' => 'daily',
                'priority' => 0.9,
            ];
        }
        unset($searchUrls);


        return $result;
    }



}
